package com.kaiyidiannao.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.kaiyidiannao.main.R;

/**
 * Created by david on 26/04/14.
 * 动态规格 LinearLayout
 */
public class SpecLinelayout extends LinearLayout{

    private String specsName; //规格
    private Object[] specsVlues;//规格数组
    private boolean isAllowSelected; //是否允许选中
    private Context context;


    public SpecLinelayout(Context context) {
        super(context);
        this.context = context;
    }

    public SpecLinelayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public SpecLinelayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }

    /**
     * 设置动态的子组件数据
     * @param specsVlues 规格名称
     * @param specsVlues 规格值数组
     * @param isAllowSelected 是否允许选中
     */
    public void setData(String specsName ,Object[] specsVlues, boolean isAllowSelected) {
        this.specsName = specsName;
        this.specsVlues = specsVlues;
        this.isAllowSelected = isAllowSelected;
        this.init();
    }

    private void init() {

        removeAllViews();

        Button button = null;
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();

        for (int j = 0; specsVlues != null && j < specsVlues.length; ++j) {

            String value = specsVlues[j].toString();
            if (value != null && !TextUtils.isEmpty(value)) {
                button = (Button) inflater.inflate(R.layout.daydaycoupon_clothes_size, null);

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                params.setMargins(0, 0 ,50, 0);
                button.setLayoutParams(params);

                button.setText(value);
                button.setSelected(false);

                addView(button);

                if (isAllowSelected) {
                    button.setOnClickListener(new ButtonOnClickListener());
                }
            }

        }
    }

    private class ButtonOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {

            for (int i = 0; i < SpecLinelayout.this.getChildCount(); ++i) {
                Button button = (Button)  SpecLinelayout.this.getChildAt(i);
                if (v == button) {
                    button.setBackgroundResource(R.drawable.daydaycoupon_detail_btn_bg_selected);
                    button.setTextColor(Color.WHITE);
                    button.setSelected(true);
                    continue;
                }
                button.setBackgroundResource(R.drawable.daydaycoupon_detail_btn_bg);
                button.setTextColor(Color.BLACK);
                button.setSelected(false);
            }
        }
    }

    /**
     * 检查规格是否选中的
     *
     * @return 有一个被选中就返回true,全部没有选中返回false
     */
    public boolean checkSelected() {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; ++i) {
            Button button = (Button) getChildAt(i);
           if (button.isSelected()) {
               return true;
           }
        }
        return false;
    }

    public int getSelectedIndex() {
        int index = 0; //没有选择默认是0
        for (int i = 0; i < SpecLinelayout.this.getChildCount(); ++i) {
            Button button = (Button)  SpecLinelayout.this.getChildAt(i);
            if (button.isSelected()) {
                index = i;
                break;
            }
        }
        return index;
    }

    public String getSpecsName() {
        return specsName;
    }
}
